//
//  EditInverterNumberView.swift
//  GreenDeal
//
//  Created by Moennig Q on 22/12/2022.
//

import UIKit
import RxCocoa
import RxSwift
import SDWebImage
class EditBarcodeViewView: GreenNaviBarView {
    private lazy var disposeBag = DisposeBag()
    let contentBackView = View(hexStr: "#FFFFFFFF")
    var barcodeImageView = UIImageView()
    var barcodeTextField = DIYTextField()
    var saveBtn = OperationButton(R.string.localizable.save(),enabled: true)
    
    override func setUpView() {
        //contentBackView
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight],radii: 16)
        contentBackView.backgroundColor = .white
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //
        barcodeImageView.contentMode = .scaleAspectFill
        barcodeImageView.drawCorner(byRoundingCorners: .allCorners,radii: 3,backColorHexStr: "#F5F5F5FF")
        self.contentBackView.addSubview(barcodeImageView)
        barcodeImageView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(200)
        }
        //
        self.contentBackView.addSubview(barcodeTextField)
        barcodeTextField.snp.makeConstraints { make in
            make.top.equalTo(barcodeImageView.snp.bottom).offset(16)
            make.left.right.equalTo(barcodeImageView)
            make.height.equalTo(44)
        }
        //
        self.contentBackView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.left.right.equalTo(barcodeImageView)
            make.height.equalTo(48)
        }
        barcodeTextField.rx.text.orEmpty.asObservable().subscribe { [weak self] str in
            if str.count > 0 {
                self?.saveBtn.enableBtn()
            }else{
                self?.saveBtn.disableBtn()
            }
        }.disposed(by: self.disposeBag)
    }
    func setData(_ imagePath: String, code: String){
        self.barcodeTextField.text = code
        //本地数据
        if imagePath.count > 0{//有路径
            if imagePath.contains("http") {
                guard let url = URL(string: imagePath) else {
                    return
                }
                self.barcodeImageView.sd_setImage(with: url, placeholderImage: nil, options: [SDWebImageOptions.scaleDownLargeImages]) { image, error, cashtype, url in
                    if let _ = image{
                        self.barcodeImageView.contentMode = .scaleAspectFill
                    }else{
                        self.barcodeImageView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
                        self.barcodeImageView.contentMode = .center
                        self.barcodeImageView.image = UIImage(named: "edit_panelnumber_loadingfailed")
                    }
                }
            }else{
                barcodeImageView.contentMode = .scaleAspectFill
                self.barcodeImageView.image = UIImage(contentsOfFile: imagePath)
            }
        }else{//无路径
            barcodeImageView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
            barcodeImageView.contentMode = .center
            self.barcodeImageView.image = UIImage(named: "edit_panelnumber_nopic")
        }
        
    }
}
